<?php
namespace App\Admin\Controllers\Consultation;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use App\Admin\Controllers\Common\Common;


class PhoneConsultationController extends Controller {
	public function index(Request $request) {
		return view("admin.consultation.phone_consultation.index",[
			"Status" => $request->get("status")
		]);
	}

	public function QueryPhone(Request $request) {
		if(!$request['phone']) {
			return ['code'=>0,'msg'=>'缺少参数','data'=>''];
		}
		$check_parent = DB::table("consultation_parent")
			->where('phone','=',$request['phone'])->value("id");
		if(!$check_parent) {
			return ['code'=>1,'msg'=>'该号码暂未注册，是否立即注册？','data'=>''];
		}
		// $primaryChildren = DB::table("consultation_parent as p")
		// 	->leftJoin("consultation_parent_to_st as t",'p.id','=','t.p_id')
		// 	->rightJoin("consultation_st as s",'t.st_id','=','s.id')
		// 	->where('p.phone','=',$request['phone'])
		// 	->where('t.is_primary','=',1)
		// 	->get(['s.*']);
		// $primaryChildren = json_decode(json_encode($primaryChildren),true);
		$children = DB::table("consultation_parent as p")
			->leftJoin("consultation_parent_to_st as t",'p.id','=','t.p_id')
			->rightJoin("consultation_st as s",'t.st_id','=','s.id')
			->where('p.phone','=',$request['phone'])
			->get(['s.*']);
		$children = json_decode(json_encode($children),true);
		if(empty($children)) {
			return ['code'=>2,'msg'=>'该号码下暂无注册的学生，是否现在注册？','data'=>""];
		}
		// if(empty($primaryChildren)) {
		// 	if(empty($children)) {
		// 		return ['code'=>2,'msg'=>'该号码下暂无注册的学生，是否现在注册？','data'=>""];
		// 	}else{
		// 		return ['code'=>3,'msg'=>'该号码下的学生暂未绑定，是否现在绑定？','data'=>$children];
		// 	}
		// }
		return ['code'=>666,'msg'=>'ok','data'=>$children];
	}

	public function getAllStData(Request $request) {
		$offset = $request['page']['pageSize'] * ($request['page']['pageNumber'] - 1);
		$total = $this->mkWhereSql($request)->count();
		$rows = $this->mkWhereSql($request)->limit($request['page']['pageSize'])->offset($offset)->get(['*',DB::raw('CEIL(DATEDIFF(NOW(),birthday)/365) as age')]);
		return ['code'=>1,'msg'=>"ok",'data'=>[
			'total' => $total,
			'rows' => $rows
		]];
	}
	private function mkWhereSql($param) {
		$db = DB::table("consultation_st");
		if($param['name']) {
			$db->where('name','like','%'.$param['name'].'%');
		}
		if($param['primary_phone']) {
			$db->where('primary_phone','like','%'.$param['primary_phone'].'%');
		}
		return $db;
	}

	/**
	 * [绑定为主要家长]
	 */
	public function bind(Request $r) {
		$phone = $r['phone'];
		$st_id = $r['st_info']['id'];
		DB::beginTransaction();
		try {
			DB::table("consultation_st")->where("id",$st_id)->update([
				'primary_phone' => $phone,
			]);
			$p_id = DB::table('consultation_parent')->where('phone',$phone)->value("id");
			DB::table("consultation_parent_to_st")
				->where("st_id",$st_id)
				->update(["is_primary" => 0]);
			DB::table('consultation_parent_to_st')->updateOrInsert(
				["p_id" => $p_id,"st_id" => $st_id],
				["is_primary" => 1]
			);
			DB::commit();
			return ['code'=>1,'msg'=>'绑定成功'];
		} catch (\Exception $e) {
			DB::rollback();
			return ['code'=>0,'msg'=>'绑定失败，请稍后...'.$e->getMessage()];
		}
	}
}
